package com.terry.test3;

import java.util.*;

/**
 *
 * 给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中，返回 true ；否则，返回 false 。
 *
 * 单词必须按照字母顺序，通过相邻的单元格内的字母构成，其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
 *
 * 输入：board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
 * 输出：true
 *
 * 用DFS更好， 这里用的BFS
 *
 * @author 小八
 * @date 2023年02月09日16:02
 */

class Point{
   public int x ;
   public int y ;
   public int index;
   public Point pre;
   public Point(int x,int y,int index,Point pre){
       this.x = x;
       this.y = y;
       this.index = index;
       this.pre = pre;
   }
}

public class Title11 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()){
            String line = in.nextLine();
            String searchLine = in.nextLine();
            char firstSearchWord = searchLine.charAt(0);
            List<Point> firstSearchList = new ArrayList<>();
            String[] split = line.substring(1, line.length() - 1).replace("],", "]|").split("\\|");
            //长
            int w = split.length;
            //宽
            int h = split[0].substring(1, split[0].length() - 1).split(",").length;
            char[][] arr = new char[w][h];
            for(int i=0;i<split.length;i++){
                String[] lines = split[i].substring(1, split[i].length() - 1).split(",");
                for(int j=0;j<lines.length;j++){
                    arr[i][j] = lines[j].charAt(1);
                    if(arr[i][j] == firstSearchWord){
                        firstSearchList.add(new Point(i,j,0,null));
                    }
                }
            }
            boolean searchResult = false;

            for (int i = 0; i < firstSearchList.size(); i++) {
                int[][] mark = new int[w][h];
                Point point = firstSearchList.get(i);
                if(find(arr,point,mark,searchLine,w,h)){
                    searchResult = true;
                    break;
                }
            }
            System.out.println(searchResult);
        }
    }

    public static boolean find(char[][] arr,Point point,int[][] mark,String searchLine,int h,int w){
        Deque<Point> deque = new ArrayDeque<>();
        deque.addLast(point);
        while(!deque.isEmpty()){
            Point p = deque.removeFirst();
            Point pre = p.pre;
            boolean breakLoop = false;
                while(pre != null){
                    if(pre.x == p.x && pre.y == p.y){
                        breakLoop = true ;
                        break;
                    }
                    pre = pre.pre;
                }
                if(breakLoop){
                    continue;
                }
                if(p.index == searchLine.length()-1){
                    return true ;
                }
                if(p.x-1>=0 && arr[p.x-1][p.y] == searchLine.charAt(p.index+1)){
                    mark[p.x-1][p.y] = 1;
                    deque.addLast(new Point(p.x-1,p.y,p.index+1,p));
                }
                if(p.x+1<h  && arr[p.x+1][p.y] == searchLine.charAt(p.index+1)){
                    mark[p.x+1][p.y] = 1;
                    deque.addLast(new Point(p.x+1,p.y,p.index+1,p));
                }
                if(p.y-1>=0  && arr[p.x][p.y-1] == searchLine.charAt(p.index+1)){
                    mark[p.x][p.y-1] = 1;
                    deque.addLast(new Point(p.x,p.y-1,p.index+1,p));
                }
                if(p.y+1<w  && arr[p.x][p.y+1] == searchLine.charAt(p.index+1)){
                    mark[p.x][p.y+1] = 1;
                    deque.addLast(new Point(p.x,p.y+1,p.index+1,p));
                }
        }
        return false;
    }
}
